package com.mark.springboot.util;

import java.awt.Color;
import java.io.FileOutputStream;
import java.util.List;

import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import com.mark.springboot.dao.QueryDao;
import com.mark.springboot.domain.TableFileds;
import com.mark.springboot.domain.Tables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * desc * 创建word文档 步骤: 1,建立文档 2,创建一个书写器 3,打开文档 4,向文档中写入数据 5,关闭文档
 *
 * @author Lixf
 * @since 2019/2/21 19:22
 **/
@Service
public class DateToWordUtil {

    @Autowired
    QueryDao queryDao;


    public void toWord(List<Tables> tables, String fileName) {// 创建word文档,并设置纸张的大小
        Document document = new Document(PageSize.A4);
        try {
            // 创建word文档
            RtfWriter2.getInstance(document, new FileOutputStream(fileName));
            document.open();
            Paragraph ph = new Paragraph();

            Font f = new Font();
            Paragraph p = new Paragraph(fileName.substring(fileName.lastIndexOf("/")+1,fileName.lastIndexOf("."))+"数据库文档", new Font(Font.NORMAL, 24, Font.NORMAL, new Color(0, 0, 0)));

            p.setAlignment(1);

            document.add(p);

            ph.setFont(f);
            for (int i = 0; i < tables.size(); i++) {
                String table_name = tables.get(i).getName();
                String table_comment = tables.get(i).getComment();

                List<TableFileds> fileds = queryDao.getTable(tables.get(i).getName());

                String all = "" + (i + 1) + table_name + ":" + table_comment;//表头

                Table table = new Table(5);//6列

                document.add(new Paragraph(""));

                table.setBorderWidth(1);

// table.setBorderColor(Color.BLACK);

                table.setPadding(0);

                table.setSpacing(0);

                /*
                 * 添加表头的元素，并设置表头背景的颜色
                 */
                Color chade = new Color(116, 116, 116);


                Cell cell = new Cell("字段名");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                cell = new Cell("类型");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);
                cell = new Cell("长度");// 单元格

                cell.setBackgroundColor(chade);

                cell.setHeader(true);

                table.addCell(cell);

                cell = new Cell("能否为空");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                cell = new Cell("备注");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                table.endHeaders();// 表头结束
                // 表格的主体
                for (int k = 0; k < fileds.size(); k++) {
                    String fieldName = fileds.get(k).getField();

                    String field = fileds.get(k).getType();

                    String fieldType = field.substring(0,field.indexOf("("));

                    String fieldSize = field.substring(field.indexOf("(")+1,field.indexOf(")"));

                    String isNull = fileds.get(k).getNull().equals("NO")?"否":"是";

                    String desc = fileds.get(k).getComment();

                    table.addCell(fieldName);

                    table.addCell(fieldType);

                    table.addCell(fieldSize);

                    table.addCell(isNull);

                    table.addCell(desc);
                }
                Paragraph pheae = new Paragraph(all);

                //设置一级标题样式
                RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;
                rtfGsBt1.setAlignment(Element.ALIGN_CENTER);
                rtfGsBt1.setStyle(Font.BOLD);
                rtfGsBt1.setSize(14);
                pheae.setFont(rtfGsBt1);
                //写入表说明
                document.add(pheae);
                //生成表格
                document.add(table);
            }
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
